<?php
/*
$qi = new sorder();
$qi->readdb('QO100');

$n = new showpdf() ;
$res = $n->outfile($qi);
$fname = 'd.pdf';
$fp = fopen($fname,'w');
fwrite($fp,$res);
fclose($fp);
*/
class showpdf {
 public $qi ;
 public $pdf;
 const ystart = 515;
 const yend = 100;
 const lineh = 15;
 const textsize = 10;
 public $ypos = self::ystart;
 public $customer;
 
 function outfile(&$obj) {
  $this->qi = &$obj ;
  $this->pdf =& new Cezpdf();
  $this->customer = freaditem("customer",$obj->header[5]);
  $this->showhead();
  $this->pdf->ezStartPageNumbers(505,709,10,'','',1);
  // Lines
  $c = 0;
  foreach ($this->qi->lines as $line) {
   $this->doline($line,true);   
  }
  $this->showfooter();
  $pdfcode = $this->pdf->ezOutput();
  return $pdfcode;
 }

	function breaktolines($str,$max) {
	 $this->pdf->openFont('fonts/Helvetica');
	 $lines = split("[\n\r]",$str);
	 $ans = array();
   $spwidth=$this->pdf->getTextWidth(self::textsize," ");
	 foreach ($lines as $interlf) {
    $e = split("[ ]",$interlf);
  	//Width of space char
	  $line = "";
  	$linesize = 0;
	  // $e is an array of given text split at convenient points
  	foreach ($e as $word) {
	   $wordwidth =  $this->pdf->getTextWidth(self::textsize,$word) ;
     // Just in case a single word is longer than max line width. Chop of end.
	   if ($wordwidth > $max) {
	    do {
	     $word=substr($word,0,-1) ;
	     $wordwidth = $this->pdf->getTextWidth(self::textsize,$word);
	    } while ($wordwidth > $max);
	   }
	 
	   // Start of a new line, no need to prepend space
	   if ($line=="") {
	    $line = $word;
	    $linesize = $wordwidth;
	   } else {
	    // Add the next word if max line not exceeded.
	    if ($linesize+$spwidth+$wordwidth > $max) {
	     // Add what we have so far to end of array
	     $ans[] = $line;
		   $line = $word;
	     $linesize = $wordwidth;
	    } else {
 	     // Add to end of line
	     $line .= " ".$word;
	     $linesize += $spwidth+$wordwidth;
	    }
	   }
	  }
	  if ($line!== "") $ans = array_merge($ans,split("[\n]",$line));
	 }
   return $ans;
	}
	
	function doline($line,$dorect = false) {
	 $text = $line[2];
	 if ($line[8]) $text .= "\n".$line[8];
	 $textwidth=350;
	 $textlines = $this->breaktolines($text,$textwidth-10);
	 $this->ypos -= self::lineh;
	 if ($this->ypos < self::yend) {
	  $this->pdf->ezNewPage();
	  $this->showhead();
	  $this->ypos = self::ystart-self::lineh;
	 }
   $linegoods = floor($line[4]*100/$line[3]*$line[6])/100;
	 $frm = $this->ypos;
	 $this->pdf->ezSetY($this->ypos); // Value
	 $this->pdf->ezText(sprintf("%2.2f",$linegoods),self::textsize,array('justification'=>'right','right'=>'26'));
	 $this->pdf->ezSetY($this->ypos); // Deliv Q
	 $this->pdf->ezText($line[6],self::textsize,array('justification'=>'right','right'=>'81'));
	 $this->pdf->ezSetY($this->ypos); // First line description
	 $this->pdf->ezText($textlines[0],self::textsize,array('justification'=>'left','left'=>'77'));
	 $this->pdf->ezSetY($this->ypos); // Code
	 $this->pdf->ezText($line[5],self::textsize,array('justification'=>'left','left'=>'17'));
	 
	 for ($i=1;$i<sizeof($textlines);$i++) {
	  $this->ypos -= self::lineh;
	  if ($this->ypos < self::yend) {
	   if ($dorect) {
	    $this->pdf->Rectangle(105,$frm,$textwidth,$this->ypos-$frm);
	    $this->pdf->Rectangle(487,$frm,55,$this->ypos-$frm);
	    $this->pdf->Rectangle(455,$frm,32,$this->ypos-$frm);
	   }
	   $this->pdf->ezNewPage();
	   $this->showhead();
	   $this->ypos = self::ystart-self::lineh;
	   $frm = self::ystart-self::lineh;
	  }
	  $this->pdf->ezSetY($this->ypos);
	  $this->pdf->ezText($textlines[$i],self::textsize,array('justification'=>'left','left'=>'79'));
	 }
	 if ($dorect == true) {
	  $this->pdf->Rectangle(45,$frm,60,$this->ypos-$frm-self::lineh);
	  $this->pdf->Rectangle(105,$frm,$textwidth,$this->ypos-$frm-self::lineh);
	  $this->pdf->Rectangle(487,$frm,55,$this->ypos-$frm-self::lineh);
	  $this->pdf->Rectangle(455,$frm,32,$this->ypos-$frm-self::lineh);
	 }
	}

	function showfooter() {
   $textlines=$this->breaktolines($this->qi->header[19],475);
	 for ($i=0;$i<sizeof($textlines);$i++) {
	  $this->ypos -= self::lineh;
	  if ($this->ypos < self::yend) {
	   $this->pdf->ezNewPage();
	   $this->showhead();
	   $this->ypos = self::ystart-self::lineh;
	   $frm = self::ystart-self::lineh;
	  }
	  $this->pdf->ezSetY($this->ypos);
	  $this->pdf->ezText($textlines[$i],self::textsize,array('justification'=>'left','left'=>'25'));
	 }
   
	 $this->pdf->setLineStyle(.5,'square','round');
	 $this->pdf->setStrokeColor(0.0,0.0,0.0);
	 $sp = 75;
	 $this->pdf->Rectangle(493,$sp-self::lineh,50,self::lineh);
	 $this->pdf->Rectangle(493,$sp-self::lineh*2,50,self::lineh);
	 $this->pdf->Rectangle(493,$sp-self::lineh*3,50,self::lineh);
	 $this->pdf->Rectangle(400,$sp-self::lineh,93,self::lineh); 
	 $this->pdf->Rectangle(400,$sp-self::lineh*2,93,self::lineh);
	 $this->pdf->Rectangle(400,$sp-self::lineh*3,93,self::lineh);
	 $this->pdf->ezSetY($sp);
   list($goods,$gst) = $this->qi->calcgst();
	 $this->pdf->ezText('Total Goods',self::textsize,array('justification'=>'left','left'=>'375'));
	 $this->pdf->ezSetY($sp);
	 $this->pdf->ezText(sprintf("%2.2f",$goods),self::textsize,array('justification'=>'right','right'=>'27'));
	 $this->pdf->ezSetY($sp-self::lineh);
	 $this->pdf->ezText('GST',self::textsize,array('justification'=>'left','left'=>'375'));
	 $this->pdf->ezSetY($sp-self::lineh);
	 $this->pdf->ezText(sprintf("%2.2f",$gst),self::textsize,array('justification'=>'right','right'=>'27'));
	 $this->pdf->ezSetY($sp-self::lineh*2);
	 $this->pdf->ezText('Grand Total',self::textsize,array('justification'=>'left','left'=>'375'));
	 $this->pdf->ezSetY($sp-self::lineh*2);
	 $this->pdf->ezText(sprintf("%2.2f",$goods+$gst),self::textsize,array('justification'=>'right','right'=>'27'));
   $this->pdf->ezSetY(50);
   $gstno = $this->qi->getgstno();
   $coreg = $this->qi->getcoreg();
	 $this->pdf->ezText("GST Reg : $gstno Co. Reg : $coreg",8,array('justification'=>'left','left'=>15));	 
	 $this->pdf->ezText("Bank details : 12-3119-0231926-00",8,array('justification'=>'left','left'=>15));	 
	}

	function showhead() {
		// Image
		$this->pdf->addJpegFromFile('cologo.JPG',45,705,100);
		// Company Name
		$br = freaditem("branch",$this->qi->header[16]);
		$addr = "$br[1]\n$br[5]\n$br[6]\n$br[7]\n$br[8]\n";
		$opts = array('shaded'=>0);$opts['showHeadings'] = 0;
		$opts['xPos'] = 200;
		$opts['fontSize'] = 11; $opts['showLines'] = 0;
		$data = array(array('aaa'=>$addr));
		$title = "";
		$this->pdf->ezTable($data,'',$title,$opts);
		
		$pos = 750;
		// Document Nos etc
		$this->pdf->setLineStyle(.5,'square','round');
		$this->pdf->setStrokeColor(0.0,0.0,0.0);
		$this->pdf->Rectangle(375,$pos-self::lineh*3,100,self::lineh);
		$this->pdf->Rectangle(475,$pos-self::lineh*3,60,self::lineh);
		$this->pdf->Rectangle(375,$pos-self::lineh,100,self::lineh);
		$this->pdf->Rectangle(475,$pos-self::lineh,60,self::lineh);
		$this->pdf->Rectangle(375,$pos-self::lineh*2,100,self::lineh);
		$this->pdf->Rectangle(475,$pos-self::lineh*2,60,self::lineh);
		$this->pdf->ezSetY(820);
		$this->pdf->ezText($this->qi->getdoctype(),30,array('justification'=>'right','right'=>'40'));
		
		$this->pdf->ezSetY($pos);
		$this->pdf->ezText('Document No',self::textsize,array('justification'=>'left','left'=>'350'));
		$this->pdf->ezSetY($pos);
		$this->pdf->ezText($this->qi->header[0],self::textsize,array('justification'=>'left','left'=>'450'));
		
		$this->pdf->ezSetY($pos-self::lineh);
		$this->pdf->ezText('Tax Date',self::textsize,array('justification'=>'left','left'=>'350'));
		$this->pdf->ezSetY($pos-self::lineh);		
		$this->pdf->ezText(isotoout($this->qi->header[17]),self::textsize,array('justification'=>'left','left'=>'450'));

		$this->pdf->ezSetY($pos-self::lineh*2);
		$this->pdf->ezText('Page',self::textsize,array('justification'=>'left','left'=>'350'));

		$this->pdf->setColor(0.9,0.9,0.9);
		$this->pdf->setStrokeColor(0,0,0);
		$this->pdf->FilledRectangle(45,self::ystart-self::lineh,60,self::lineh);
		$this->pdf->FilledRectangle(105,self::ystart-self::lineh,350,self::lineh);
		$this->pdf->FilledRectangle(487,self::ystart-self::lineh,55,self::lineh);
		$this->pdf->FilledRectangle(455,self::ystart-self::lineh,32,self::lineh);
		$this->pdf->setColor(0,0,0);
		$this->pdf->Rectangle(45,self::ystart-self::lineh,60,self::lineh);
		$this->pdf->Rectangle(105,self::ystart-self::lineh,350,self::lineh);
		$this->pdf->Rectangle(487,self::ystart-self::lineh,55,self::lineh);
		$this->pdf->Rectangle(455,self::ystart-self::lineh,32,self::lineh);
		$this->pdf->ezSetY(self::ystart);
		$this->pdf->ezText('Code',self::textsize,array('justification'=>'left','left'=>'20'));
		$this->pdf->ezSetY(self::ystart);
		$this->pdf->ezText('Description',self::textsize,array('justification'=>'left','left'=>'80'));
		$this->pdf->ezSetY(self::ystart);
		$this->pdf->ezText('Qty',self::textsize,array('justification'=>'right','right'=>'87'));
		$this->pdf->ezSetY(self::ystart);
		$this->pdf->ezText('Price',self::textsize,array('justification'=>'right','right'=>'28'));
    $h = $this->qi->header;
    $c = $this->customer;
    $this->pdf->ezSetY(660);
		$this->pdf->ezText("$c[1]\n$c[5]\n$c[6]\n$c[7]\n$c[8]\n$c[9]\n$c[10]",10, array('justification'=>'left','left'=>'15'));
    $this->pdf->ezSetY(660);
		$this->pdf->ezText("$h[6]\n$h[7]\n$h[8]\n$h[9]\n$h[10]",10, array('justification'=>'left','left'=>'265'));
	}


}
?>